#include "u8g2PageProtocolShow.h"
#include "u8g2Helper.h"
#include "u8g2Common.h"
#include "env.h"
#include "uiService.h"
#include "stdio.h"
#define U8G2_PAGE_PROTOCOL_SHOW_BEGIN_HOLD_TIME 1000
#define U8G2_PAGE_PROTOCOL_SHOW_DETAIL_HOLD_TIME 1000
#define U8G2_SHOW_COUNTER(value) (value / UI_SERVICE_DURATION)
typedef enum
{
    E_U8G2_PROTOCOL_SHOW_BEGIN = 0,
    E_U8G2_PROTOCOL_SHOW_STAGE1,
    E_U8G2_PROTOCOL_SHOW_STAGE2,
    E_U8G2_PROTOCOL_SHOW_STAGE3,
} U8g2ProtoclShowStageEnum;

static U8g2ProtoclShowStageEnum stage = E_U8G2_PROTOCOL_SHOW_BEGIN;

void drawProtoclDetial(u8g2_t *u8g2)
{
    char volt[9];
    char curr[9];
    sprintf(volt, "%.2f V", env.sourceCapVoltage);
    sprintf(curr, "%.2f A", env.sourceCapCurrent);
    u8g2_SetFont(u8g2, u8g2_font_spleen8x16_mu);
    if (env.chargerMode == E_CHARGE_MODE_PD)
    {
        u8g2_DrawStr(u8g2, 64 - (u8g2_GetStrWidth(u8g2, volt) / 2), 42, volt);
        u8g2_DrawStr(u8g2, 64 - (u8g2_GetStrWidth(u8g2, curr) / 2), 54, curr);
    }
    else if (env.chargerMode == E_CHARGE_MODE_QC)
    {
        u8g2_DrawStr(u8g2, 64 - (u8g2_GetStrWidth(u8g2, volt) / 2), 42, volt);
        u8g2_DrawStr(u8g2, 64 - (u8g2_GetStrWidth(u8g2, "MPPT") / 2), 54, "MPPT");
    }
    else if (env.chargerMode == E_CHARGE_MODE_NONE)
    {
        u8g2_DrawStr(u8g2, 64 - (u8g2_GetStrWidth(u8g2, volt) / 2), 42, volt);
        u8g2_DrawStr(u8g2, 64 - (u8g2_GetStrWidth(u8g2, "MPPT") / 2), 54, "MPPT");
    }
}

static U8g2EaseMoveHandler easeHandler;
static int showHolder = 0;
static int dd = 0;
static unsigned int tmp = 0;
BOOL u8G2ProtoclShow(u8g2_t *u8g2)
{
    BOOL ret = FALSE;
    switch (stage)
    {
    case E_U8G2_PROTOCOL_SHOW_BEGIN:
        drawTitle(u8g2, 64, 32, env.chargerMode);
        showHolder++;
        if (showHolder > U8G2_SHOW_COUNTER(U8G2_PAGE_PROTOCOL_SHOW_BEGIN_HOLD_TIME))
        {
            showHolder = 0;
            u8g2EaseInit(&easeHandler, 1, 3, 0, 16);
            stage = E_U8G2_PROTOCOL_SHOW_STAGE1;
        }
        break;
    case E_U8G2_PROTOCOL_SHOW_STAGE1:
        if (dd >= 4)
        {
            dd = 0;
            tmp = u8g2Ease(&easeHandler);
        }
        else
        {
            dd++;
        }
        drawTitle(u8g2, 64, 32 - tmp, env.chargerMode);
        if (tmp == 16)
        {
            stage = E_U8G2_PROTOCOL_SHOW_STAGE2;
        }
        break;
    case E_U8G2_PROTOCOL_SHOW_STAGE2:
        drawTitle(u8g2, 64, 16, env.chargerMode);
        drawProtoclDetial(u8g2);
        showHolder++;
        if (showHolder > U8G2_SHOW_COUNTER(U8G2_PAGE_PROTOCOL_SHOW_DETAIL_HOLD_TIME))
        {
            showHolder = 0;
            u8g2EaseInit(&easeHandler, 1, 6, 0, 48);
            stage = E_U8G2_PROTOCOL_SHOW_STAGE3;
            tmp = 0;
            dd = 0;
        }
        break;
    case E_U8G2_PROTOCOL_SHOW_STAGE3:
        if (dd >= 4)
        {
            dd = 0;
            tmp = u8g2Ease(&easeHandler);
        }
        else
        {
            dd++;
        }
        drawTitle(u8g2, 64 - tmp, 16, env.chargerMode);
        if (tmp == 48)
        {
            ret = TRUE;
        }
    default:
        break;
    }
    return ret; // return TRUE to EXIT LOOP; return FALSE to continue
}
